package com.ocm.controller.mapper;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;

import com.ocm.core.domain.Permission;
import com.ocm.core.domain.UserDetailType;
import com.ocm.core.dto.GroupDto;
import com.ocm.core.dto.UserInfoDto;

@Component("jsonMapping")
public class JsonMappingImpl implements JsonMapping {

   @Autowired
   private ResourceBundleMessageSource messageSource;

   public JSONArray convertSearchGroups(List<GroupDto> groupDtos) {
      JSONArray jsonArray = new JSONArray();
      for (GroupDto groupDto : groupDtos){
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("id", groupDto.getId());
         jsonObject.put("displayName", groupDto.getUsername());
         jsonObject.put("type", "group");
         jsonArray.add(jsonObject);
      }
      return jsonArray;
   }

   public JSONArray convertSearchUsers(List<UserInfoDto> usersDtos) {
      JSONArray jsonArray = new JSONArray();
      for (UserInfoDto userInfoDto : usersDtos){
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("id", userInfoDto.getId());
         jsonObject.put("displayName", userInfoDto.getUsername());
         if (UserDetailType.USER.getType() == userInfoDto.getType()){
            jsonObject.put("type", "user");
         }else {
            jsonObject.put("type", "group");
         }
         jsonArray.add(jsonObject);
      }
      return jsonArray;
   }

   public JSONArray convertPermisson(Permission[] permissions) {
      JSONArray jsonArray = new JSONArray();
      for (Permission permission : permissions){
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("id", permission.getPermission());
         switch (permission) {
            case CREATEINSTANCE:
               jsonObject.put("displayName", messageSource.getMessage("security.createInstance", null, null));
               break;
            case DELETE:
               jsonObject.put("displayName", messageSource.getMessage("security.delete", null, null));
               break;
            case DOWNLOAD:
               jsonObject.put("displayName", messageSource.getMessage("security.download", null, null));
               break;
            case MODIFYPERMISSON:
               jsonObject.put("displayName", messageSource.getMessage("security.modifypermission", null, null));
               break;
            case MODIFYPROPERTIES:
               jsonObject.put("displayName", messageSource.getMessage("security.modifyproperties", null, null));
               break;
            case VIEWPERMISSION:
               jsonObject.put("displayName", messageSource.getMessage("security.viewpermission", null, null));
               break;
            case VIEWPROPERTIES:
               jsonObject.put("displayName", messageSource.getMessage("security.viewproperties", null, null));
               break;
            case LINK:
               jsonObject.put("displayName", messageSource.getMessage("security.linkproperties", null, null));
               break;
         }
         jsonArray.add(jsonObject);
      }
      return jsonArray;
   }
}
